package com.kly.user.serviceImpl.traffic.forceConnect;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.collect.Maps;
import com.jsoniter.output.JsonStream;
import com.kly.user.db.dao.CustomersUserDao;
import com.kly.user.db.dao.UserChatterMappingDao;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.dao.UserWlmDao;
import com.kly.user.db.dbo.FilterOnlineUserCustomerDBO;
import com.kly.user.db.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FcTrafficHandler {

    public static Log logger = LogFactory.getLog(FcTrafficHandler.class);
    public static Map<String, FcBase> fcBaseMap = new HashMap<>();

    @Autowired
    private UserDao userDao;


    @Resource
    UserWlmDao userWlmDao;


    @Resource
    UserChatterMappingDao userChatterMappingDao;


    @Autowired
    CustomersUserDao customersUserDao;


    public List<IssueUsersDetailEntity> filterAllApps(String chatterUserCode, List<IssueUsersDetailEntity> issueUsersEntityList) {
        logger.info("==chatterOnlineUserListData  ==,filterAllApps size:{}", issueUsersEntityList.size());
        if (CollectionUtils.isEmpty(issueUsersEntityList)) {
            return issueUsersEntityList;
        }
        Map<String, List<IssueUsersDetailEntity>> appsAndUses = issueUsersEntityList.stream().collect(Collectors.groupingBy(IssueUsersDetailEntity::getAppCode));
//        logger.info("==chatterOnlineUserListData  ==,appsAndUses data:{}", JsonStream.serialize(appsAndUses));
        List<IssueUsersDetailEntity> res = new ArrayList<>();
        appsAndUses.forEach((k, v) -> {
            List<IssueUsersDetailEntity> userEntities = appsAndUses.get(k);
            List<IssueUsersDetailEntity> filteredRes = fcBaseMap.get(k).getFilteredRes(userEntities);
            res.addAll(filteredRes);
        });

        List<String> validUserCodes = res.parallelStream().map(IssueUsersDetailEntity::getUserCode).collect(Collectors.toList());
        logger.info("==chatterOnlineUserListData  ==,validUserCodes data:{}", JsonStream.serialize(validUserCodes));
        List<IssueUsersDetailEntity> finalRes = new ArrayList<>();
        issueUsersEntityList.forEach(c -> {
            if (validUserCodes.contains(c.getUserCode())) {
                finalRes.add(c);
            }
        });

        logger.info("==chatterOnlineUserListData  ==,finalRes size:{}", finalRes.size());
//        return filterCustomer(finalRes, chatterUserCode);
        return finalRes;
    }

    private List<IssueUsersDetailEntity> filterCustomer(List<IssueUsersDetailEntity> userDialogDtos, String chatterCode) {
        logger.info("==chatterOnlineUserListData  ==,默认拿到的数量:{}", userDialogDtos.size());
        if (CollectionUtils.isEmpty(userDialogDtos)) {
            return new ArrayList<>();
        }

        List<String> users = userDialogDtos.parallelStream().map(IssueUsersDetailEntity::getUserCode).collect(Collectors.toList());
        //找到当前主播的model数量
        List<UserChatterMappingEntity> userChatterMappingEntities = userChatterMappingDao.queryByChatterUserCode(chatterCode);
        if (CollectionUtils.isEmpty(userChatterMappingEntities)) {
            logger.error("==chatterOnlineUserListData  ==,当前主播没有model:{}", userDialogDtos.size());
            return new ArrayList<>();
        }
        logger.info("==chatterOnlineUserListData  找到当前model的model数==,默认拿到的数量:{}", userDialogDtos.size());
        List<String> modelCodes = userChatterMappingEntities.parallelStream().map(UserChatterMappingEntity::getModelUserCode).collect(Collectors.toList());
        int modelsCountOfChatter = userChatterMappingEntities.size();

        //找到当列表中用户的customer_code
        logger.info("==chatterOnlineUserListData  找到当前model的model数2==,size:{}", modelCodes.size());
        List<CustomersUserEntity> customers = customersUserDao.getCustomers(users);
        if (CollectionUtils.isEmpty(customers)) {
            logger.error("==chatterOnlineUserListData  ==,customers为空:{}", userDialogDtos.size());
            return new ArrayList<>();
        }

        logger.info("==chatterOnlineUserListData  customers==,size:{}", customers.size());

        List<String> customerCodes = customers.parallelStream().map(CustomersUserEntity::getCustomerCode).distinct().collect(Collectors.toList());
        Map<String, String> userAndCustomerMap = new HashMap<>();
        customers.forEach(c -> {
            userAndCustomerMap.put(c.getUserCode(), c.getCustomerCode());
        });

        logger.info("==chatterOnlineUserListData  customers==,customers:{}", 1);
        if (CollectionUtils.isEmpty(customers)) {
            logger.error("==chatterOnlineUserListData  ==,filterOnlineUserCustomerDBOS为空:{}", userDialogDtos.size());
            return new ArrayList<>();
        }

        List<FilterOnlineUserCustomerDBO> filterOnlineUserCustomerDBOS = userWlmDao.listOnlineUserChats(customerCodes, modelCodes);
        Map<String, FilterOnlineUserCustomerDBO> customerDBOMap = new HashMap<>();
        for (FilterOnlineUserCustomerDBO filterOnlineUserCustomerDBO : filterOnlineUserCustomerDBOS) {
            customerDBOMap.put(filterOnlineUserCustomerDBO.getCustomerCode(), filterOnlineUserCustomerDBO);
        }
        logger.info("==chatterOnlineUserListData  5555==,customers:{}", 2);
        List<IssueUsersDetailEntity> userDialogDtosFinal = new ArrayList<>();
        userDialogDtos.forEach(u -> {
            if (!userAndCustomerMap.containsKey(u.getUserCode())) {
                logger.error("==chatterOnlineUserListData  ==,没有客户 userCode:{}", u.getUserCode());
                return;
            }else{
                logger.info("==chatterOnlineUserListData  ==,有客户 userCode:{}", u.getUserCode());
            }

            String customerCode = userAndCustomerMap.get(u.getUserCode());
            if (!customerDBOMap.containsKey(customerCode)) {
                logger.info("==chatterOnlineUserListData  ==,没有找到和这个主播关联的记录 userCode:{}", u.getUserCode());
                userDialogDtosFinal.add(u);
                return;
            }else{
                logger.info("==chatterOnlineUserListData  ==,找到和这个主播关联的记录 userCode:{}", u.getUserCode());
            }
            FilterOnlineUserCustomerDBO thisCustomerData = customerDBOMap.get(customerCode);
            if (!thisCustomerData.hasAllModelsUsedUp(modelsCountOfChatter)) {
                logger.info("==chatterOnlineUserListData  ==,主播和当前用户还有未建联的model userCode:{}", u.getUserCode());
                userDialogDtosFinal.add(u);
            }else{
                logger.info("==chatterOnlineUserListData  ==,主播和当前用户meiyou 未建联的model userCode:{}", u.getUserCode());
            }
        });

        logger.info("==chatterOnlineUserListData  66666==,customers:{}", 5);
        return userDialogDtosFinal;
    }

}
